//package com.example.event_resource_java.starinside.shiro.config;
//import com.example.event_resource_java.starinside.shiro.cache.SpringCacheManagerWrapper;
//import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
//import com.example.event_resource_java.authority.server.UserServer;
//import com.example.event_resource_java.starinside.config.BDSessionListener;
//import com.example.event_resource_java.starinside.session.RedisSessionDAO;
//import com.example.event_resource_java.starinside.shiro.JWTAuthorizingRealm;
//import com.example.event_resource_java.starinside.shiro.SysUserAuthorizingRealm;
//import com.example.event_resource_java.starinside.spring.SpringContextHolder;
//import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
//import org.apache.shiro.cache.CacheManager;
//import org.apache.shiro.crypto.hash.Sha256Hash;
//import org.apache.shiro.mgt.SecurityManager;
//import org.apache.shiro.session.SessionListener;
//import org.apache.shiro.session.mgt.SessionManager;
//import org.apache.shiro.session.mgt.eis.SessionDAO;
//import org.apache.shiro.spring.LifecycleBeanPostProcessor;
//import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
//import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
//import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
//import org.apache.shiro.web.servlet.SimpleCookie;
//import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
//import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.DependsOn;
//import org.springframework.data.redis.connection.RedisConnectionFactory;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.serializer.StringRedisSerializer;
//
//import javax.servlet.Filter;
//import java.util.*;
//
///**
// * <pre>
// * . cache ehcache
// * . realm(cache)
// * . securityManager（realm）
// * . ShiroFilterFactoryBean 注册
// *
// * </pre>
// * <small> 2018年4月18日 | Aron</small>
// */
//@Configuration
//public class ShiroConfiguration {
//
//    @Bean
//    SessionDAO sessionDAO(ShiroProperties config) {
//        RedisSessionDAO sessionDAO = new RedisSessionDAO(config.getSessionKeyPrefix());
//        return sessionDAO;
//    }
//
//    @Bean
//    public SimpleCookie sessionIdCookie(ShiroProperties shiroConfigProperties) {
//        return new SimpleCookie(shiroConfigProperties.getJsessionidKey());
//    }
//
//    @Bean
//    public RedisTemplate<Object, Object> redisTemplate( RedisConnectionFactory redisConnectionFactory) {
//        RedisTemplate<Object, Object> template = new RedisTemplate<>();
//        template.setConnectionFactory(redisConnectionFactory);
//        template.setKeySerializer(new StringRedisSerializer());
//        return template;
//    }
//
//    @Bean
//    public SessionManager sessionManager(SessionDAO sessionDAO, SimpleCookie simpleCookie) {
//        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
//        sessionManager.setSessionIdCookie(simpleCookie);
//
//        Collection<SessionListener> sessionListeners = new ArrayList<>();
//        sessionListeners.add(new BDSessionListener());
//        sessionManager.setSessionListeners(sessionListeners);
//        sessionManager.setSessionDAO(sessionDAO);
//        return sessionManager;
//    }
//
//    @Bean(name = "shiroCacheManager")
//    @DependsOn({"springContextHolder", "cacheConfiguration"})
//    public CacheManager cacheManager() {
//        SpringCacheManagerWrapper springCacheManager = new SpringCacheManagerWrapper();
//        org.springframework.cache.CacheManager cacheManager = SpringContextHolder.getBean(org.springframework.cache.CacheManager.class);
//        springCacheManager.setCacheManager(cacheManager);
//        return springCacheManager;
//    }
//
//    @Bean
//    JWTAuthorizingRealm jwtAuthorizingRealm() {
//        JWTAuthorizingRealm realm = new JWTAuthorizingRealm();
//        realm.setCachingEnabled(true);
//        realm.setAuthorizationCachingEnabled(true);
//        return realm;
//    }
//
//    @Bean
//    SysUserAuthorizingRealm sysUserAuthorizingRealm( UserServer userService){
//        SysUserAuthorizingRealm realm = new SysUserAuthorizingRealm( userService);
//        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
//        credentialsMatcher.setHashAlgorithmName(Sha256Hash.ALGORITHM_NAME);
//        realm.setCredentialsMatcher(credentialsMatcher);
//        realm.setCachingEnabled(true);
//        realm.setAuthorizationCachingEnabled(true);
//        return realm;
//    }
//
//
//    @Bean
//    SecurityManager securityManager(SessionManager sessionManager , CacheManager cacheManager, JWTAuthorizingRealm realm1, SysUserAuthorizingRealm realm2) {
//        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
//        manager.setCacheManager(cacheManager);
//        manager.setRealms(Arrays.asList(realm1, realm2));
//        manager.setSessionManager(sessionManager);
//        return manager;
//    }
//
//
//    @Bean
//    ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
//        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
//
//        // 添加jwt过滤器
//        Map<String, Filter> filterMap = new HashMap<>();
//       // filterMap.put("jwt", new JWTAuthenticationFilter());
//        shiroFilterFactoryBean.setFilters(filterMap);
//
//        shiroFilterFactoryBean.setSecurityManager(securityManager);
//        shiroFilterFactoryBean.setLoginUrl("/login");
//        shiroFilterFactoryBean.setSuccessUrl("/index");
//        shiroFilterFactoryBean.setUnauthorizedUrl("/shiro/405");
//        LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
//        // 放行
//        filterChainDefinitionMap.put("/code/**", "anon");
//        filterChainDefinitionMap.put("/testResourceCon", "anon");
//        filterChainDefinitionMap.put("/resource/**", "anon");
//        filterChainDefinitionMap.put("/excels/**", "anon");
//        filterChainDefinitionMap.put("/statistic/**", "anon");
//        filterChainDefinitionMap.put("/forgetPassword", "anon");
//
//        filterChainDefinitionMap.put("/course/**", "anon");
//
//        filterChainDefinitionMap.put("/class/*", "anon");
//        filterChainDefinitionMap.put("/shiro/**", "anon");
//        filterChainDefinitionMap.put("/login", "anon");
//        filterChainDefinitionMap.put("/css/**", "anon");
//        filterChainDefinitionMap.put("/js/**", "anon");
//        filterChainDefinitionMap.put("/fonts/**", "anon");
//        filterChainDefinitionMap.put("/img/**", "anon");
//        filterChainDefinitionMap.put("/lib/**", "anon");
//        filterChainDefinitionMap.put("/docs/**", "anon");
//        filterChainDefinitionMap.put("/druid/**", "anon");
//        filterChainDefinitionMap.put("/upload/**", "anon");
//        filterChainDefinitionMap.put("/files/**", "anon");
//        filterChainDefinitionMap.put("/test/**", "anon");
//        filterChainDefinitionMap.put("/logout", "logout");
//        filterChainDefinitionMap.put("/msgTemplate/", "anon");
//        filterChainDefinitionMap.put("/", "anon");
//        filterChainDefinitionMap.put("/**", "authc");
//        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
//        return shiroFilterFactoryBean;
//    }
//
//    @Bean
//    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
//        return new LifecycleBeanPostProcessor();
//    }
//
//    @Bean
//    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
//        DefaultAdvisorAutoProxyCreator proxyCreator = new DefaultAdvisorAutoProxyCreator();
//        proxyCreator.setProxyTargetClass(true);
//        return proxyCreator;
//    }
//
//    @Bean
//    public ShiroDialect shiroDialect() {
//        return new ShiroDialect();
//    }
//
//    @Bean
//    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
//        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
//        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
//        return authorizationAttributeSourceAdvisor;
//    }
//
//}
